import {Component} from '@angular/core';
import {IonicPage, NavController, NavParams} from 'ionic-angular';
import {ValidateProvider} from "../../providers/validate/validate";
import {ConfigProvider} from "../../providers/config/config";
import {StorageProvider} from "../../providers/storage/storage";
import {HttpServicesProvider} from "../../providers/http-services/http-services";
import {UtilsProvider} from "../../providers/utils/utils";

@IonicPage({
    segment: 'member/message',
    defaultHistory: ['MemberPage']
})
@Component({
    selector: 'page-person-message',
    templateUrl: 'person-message.html',
})
export class PersonMessagePage {
    page:number = 1;
    constructor(
        public navCtrl: NavController,
        public navParams: NavParams,
        public validate: ValidateProvider,
        public config: ConfigProvider,
        public storage: StorageProvider,
        public httpService: HttpServicesProvider,
        public util: UtilsProvider
    ) {
        if (!this.validate.checkLogin()) {
            this.navCtrl.push('LoginPage');
        }
    }

    //判断用户是否登录
    ionViewCanEnter(){
        return this.validate.checkLogin();
    }

    userInfo = this.config.userInfoHolder;
    ionViewWillEnter() {
        if (!this.validate.checkLogin()) {//登录
            this.navCtrl.push('LoginPage', {'backPage': 'MemberPage'});
        } else {
            this.getData(null);
        }
    }

    infos = [];

    getData(infiniteScroll) {
        let tokenInfo = this.storage.get('tokenInfo');
        if (tokenInfo) {
            tokenInfo = JSON.parse(tokenInfo);
            let apiUrl = "/User_info/my_information/token/" + tokenInfo['token'] + '/page/' + this.page;
            this.httpService.requestData(apiUrl, response => {
                this.infos=this.infos.concat(response['data']['lists']);
                this.page++;
                if (infiniteScroll) {
                    infiniteScroll.complete();
                    /*请求完成数据更新页面*/
                    if (response['data']['lists'].length < response['data']['page_size']) {   /*没有数据 禁用上拉更新*/
                        infiniteScroll.enable(false);
                    } else {
                        this.infos=this.infos.concat(response['data']['lists']);
                    }
                }
            });
        }
    }

    doInfinite(infiniteScroll) {
        this.getData(infiniteScroll);
    }


    /**
     * 删除发布的消息
     * */
    delMessage(info,i){
        //是否是有偿分享信息
        if(info.click_pay==1){            
            this.util.presentConfirm('这是有偿分享信息，确定要删除吗？删除后余额将退还到你的钱包',()=>{
                let apiUrl = "/user_info/del_information/amoy_id/"+ info.amoy_id +"/token/" + this.config.getToken();
                this.httpService.requestData(apiUrl, response => {
                    if(response.error == 200){
                        this.infos.splice(i,1);
                        this.util.presentAlert('信息删除成功');
                    }else{
                        this.util.presentAlert('信息删除失败');
                    }
                });
            });
        }else{
            this.util.presentConfirm('确定要删除该信息吗？',()=>{
                let apiUrl = "/user_info/del_information/amoy_id/"+ info.amoy_id +"/token/" + this.config.getToken();
                this.httpService.requestData(apiUrl, response => {
                    if(response.error == 200){
                        this.infos.splice(i,1);
                        this.util.presentAlert('信息删除成功');
                    }else{
                        this.util.presentAlert('信息删除失败');
                    }
                });
            });
        }
        
    }

    /**
     * 跳转到信息详情
     * */
    goInformationDetail(amoy_id){
        this.navCtrl.push('InformationDetailPage', {id: amoy_id});
    }


}
